package com.example.red_tea_house.moudle.Home.view;

import android.Manifest;
import android.content.Context;
import android.content.Intent;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.red_tea_house.MyApplication;
import com.example.red_tea_house.R;

import com.example.red_tea_house.entity.UserBean;
import com.example.red_tea_house.moudle.Home.adapter.HomeFragmentPagersAdapter;
import com.example.red_tea_house.moudle.LoginAndRegister.view.LoginActivity;

import com.example.red_tea_house.moudle.Write.view.EditActivity;

import com.example.red_tea_house.util.ToastUtil;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout linearLayoutWrite,linearLayoutHome, linearLayoutRead;
    private ImageView viewWrite,viewHome, viewRead,viewCurrent;
    private TextView textHome,textRead,textWrite;

    private ViewPager2 viewPager;
    ArrayList<Fragment> fragmentList;

    private HomeFragment homeFragment=HomeFragment.newInstance();


    Context context= MyApplication.getContext();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_layout);

        LitePal.getDatabase();//创建数据库

        skipIfLogined();

        initViewPager();
        iniTabView();
    }

    private void skipIfLogined(){
        List<UserBean> userBean= LitePal.findAll(UserBean.class);
        if(userBean.isEmpty()){
            GetintoLoginPage();
        }
    }
    private void GetintoLoginPage(){
        Intent intent=new Intent(MainActivity.this, LoginActivity.class);
        startActivityForResult(intent,1);
    }
    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        switch (requestCode){
            case 1:
                if(resultCode==RESULT_OK){
                    Boolean LoginResult=data.getBooleanExtra("LoginState",false);
                    if(LoginResult)
                        changeHomePageInformation();
                        ToastLoginSuccess();
                }
        }
    }
    private void changeHomePageInformation(){
        homeFragment.getUserData();
    }
    private void ToastLoginSuccess(){
        ToastUtil.showToast(context,"登录成功！");
    }

    private void initViewPager() {
        viewPager =findViewById(R.id.homeViewpager);
        setFragment();
        HomeFragmentPagersAdapter pagerAdapter = new HomeFragmentPagersAdapter(getSupportFragmentManager(), getLifecycle(),fragmentList);
        viewPager.setAdapter(pagerAdapter);
        //滑动效果
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position){
                super.onPageSelected(position);
                changeTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state){
                super.onPageScrollStateChanged(state);
            }
        });
    }
    private void setFragment(){
        fragmentList=new ArrayList<>();
        fragmentList.add(WriteFragment.newInstance());
        fragmentList.add(homeFragment);
        fragmentList.add(ReadFragment.newInstance());
    }

    //初始化底部btn和图像，用于点击后的变换
    private void iniTabView() {
        initLinearLayoutAndSetClickListener();
        initButtonAndSetClickListener();
        initTextAndSetClickListener();
        setDefaultView();
    }
    private void initLinearLayoutAndSetClickListener(){
        linearLayoutWrite = findViewById(R.id.write_layout_btn);
        linearLayoutWrite.setOnClickListener(this);
        linearLayoutHome = findViewById(R.id.home_layout_btn);
        linearLayoutHome.setOnClickListener(this);
        linearLayoutRead = findViewById(R.id.read_layout_btn);
        linearLayoutRead.setOnClickListener(this);
    }
    private void initButtonAndSetClickListener(){
        viewWrite = findViewById(R.id.write_layout_btn_view);
        viewWrite.setOnClickListener(this);
        viewHome = findViewById(R.id.home_layout_btn_view);
        viewHome.setOnClickListener(this);
        viewRead = findViewById(R.id.read_layout_btn_view);
        viewRead.setOnClickListener(this);
    }
    private void initTextAndSetClickListener(){
        textHome=findViewById(R.id.home_text);
        textHome.setOnClickListener(this);
        textRead=findViewById(R.id.read_text);
        textRead.setOnClickListener(this);
        textWrite=findViewById(R.id.write_text);
        textWrite.setOnClickListener(this);
    }
    private void setDefaultView(){
        viewRead.setSelected(true);
        viewCurrent =viewRead;
        viewPager.setCurrentItem(2);
    }
    /*
    * 点击切换页面
    * */
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.write_layout_btn:
            case R.id.write_layout_btn_view:
            case R.id.write_text:
                changeTab(0);
                break;
            case R.id.home_layout_btn:
            case R.id.home_layout_btn_view:
            case R.id.home_text:
                changeTab(1);
                break;
            default:
                changeTab(2);
                break;
        }
    }
    //改变底部btn颜色,点击按钮跳转页面
    private void changeTab(int position) {
        viewCurrent.setSelected(false);
        switch (position){
            case 0:
                viewWrite.setSelected(true);
                viewCurrent = viewWrite;
                viewPager.setCurrentItem(0);
                break;
            case 1:
                viewHome.setSelected(true);
                viewCurrent = viewHome;
                viewPager.setCurrentItem(1);
                break;
            case 2:
                viewRead.setSelected(true);
                viewCurrent = viewRead;
                viewPager.setCurrentItem(2);
                break;
        }
    }

    public void turnIntoEditPage(){
        Intent intent=new Intent(MainActivity.this, EditActivity.class);
        startActivity(intent);
    }
}
